#!/bin/bash
# RK3588视频流客户端启动脚本 - 纯传输优化版

# 系统优化 - 专注传输性能
echo 3 > /proc/sys/kernel/printk 2>/dev/null || true

# 网络优化
echo 131072 > /proc/sys/net/core/rmem_max 2>/dev/null || true
echo 131072 > /proc/sys/net/core/wmem_max 2>/dev/null || true

echo "[启动] RK3588纯传输视频流客户端"
echo "========================================="

# 基础环境检查
echo "[检查] 环境检查..."

if [ ! -e "/dev/video41" ]; then
    echo "[错误] 摄像头设备不存在"
    exit 1
fi

if ! command -v gst-launch-1.0 >/dev/null 2>&1; then
    echo "[错误] GStreamer未安装" 
    echo "请运行: sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-*"
    exit 1
fi

echo "[成功] 基础环境检查完成"

# 彻底清理端口和进程
echo "[清理] 清理旧进程..."
pkill -f "gst-launch" >/dev/null 2>&1
pkill -f "rk3588_client" >/dev/null 2>&1
fuser -k 8080/tcp >/dev/null 2>&1
sleep 1

# 设置设备权限
chmod 666 /dev/video41 2>/dev/null

echo "[启动] 启动纯传输客户端..."
exec python3 rk3588_client.py 192.168.233.187 9000